package elements

import (
	"yizhuoyan/go-study/pdfgo/handyio"
)

type CrossReferenceTableElement struct {
	//第0个交叉表为初始交叉表
	section  *CrossReferenceSection
	Previous *CrossReferenceTableElement
}

func (this *CrossReferenceTableElement) GetObjIndexAndGn(objNum int) (int64, int) {
	//从最新的交叉表开始查询
	if objIndex, gn := this.section.GetObjIndexAndGn(objNum); objIndex > 0 {
		return objIndex, gn
	}
	if this.Previous != nil {
		return this.Previous.GetObjIndexAndGn(objNum)
	}
	return -1, 0
}
func (this *CrossReferenceTableElement) Write(w handyio.PdfRawWriter) error {
	if err := this.section.WriteSection(w); err != nil {
		return err
	}
	return nil
}

func (this *CrossReferenceTableElement) Read(r handyio.PdfRawReader) error {
	startAt := r.IndexCurrent()
	this.section = new(CrossReferenceSection)
	if err := this.section.ReadSection(r); err != nil {
		//说明已解析完毕
		if r.IsNotSupportError(err) {
			return nil
		}
		_ = r.IndexMoveTo(startAt)
		return err
	}

	return nil

}
